from django.db import models
from users.models import UserModel
from goods.models import SKU


class OrderModel(models.Model):
    PAY_METHOD = (
        (1, '支付宝'),
        (2, '微信支付'),
        (3, '银联支付'),
        (4, '货到付款')
    )
    PAY_STATUS = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成'),
    )
    order_id = models.CharField(max_length=64, primary_key=True, verbose_name="订单编号")
    user = models.ForeignKey(UserModel, related_name='orders', on_delete=models.PROTECT, verbose_name="下单用户")
    total_count = models.IntegerField(default=1, verbose_name="商品总数")
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品总金额")
    freight = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="运费")
    pay_method = models.SmallIntegerField(default=1, choices=PAY_METHOD, verbose_name="支付方式")
    # 1-待支付，2-待发货，3-待收货，4-待评价，5-已完成
    status = models.SmallIntegerField(default=1, choices=PAY_STATUS, verbose_name="订单状态")
    created_time = models.DateTimeField(verbose_name="订单的创建时间", auto_now_add=True)

    class Meta:
        db_table = "tb_order"
        verbose_name_plural = '订单基本信息'

    def __str__(self):
        return self.order_id


class OrderGoods(models.Model):
    """订单商品"""
    # 购买的商品
    sku = models.ForeignKey(SKU, on_delete=models.PROTECT, verbose_name="auth_group订单商品")
    count = models.IntegerField(default=1, verbose_name="数量")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="单价")
    order = models.ForeignKey(OrderModel, related_name='skus', on_delete=models.CASCADE, verbose_name="订单")
    comment = models.TextField(default="", verbose_name="评价信息")
    score = models.SmallIntegerField(default=5, verbose_name='评分')
    is_anonymous = models.BooleanField(default=False, verbose_name='是否匿名评价')
    is_commented = models.BooleanField(default=False, verbose_name='是否评价了')

    class Meta:
        db_table = "tb_order_goods"
        verbose_name_plural = '订单商品'

    def __str__(self):
        return self.sku.name


class Payment(models.Model):
    """支付信息"""
    trade_id = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="支付编号")
    order = models.ForeignKey(OrderModel, on_delete=models.CASCADE, verbose_name='订单')

    class Meta:
        db_table = 'tb_payment'
        verbose_name_plural = '支付信息'


class Addr(models.Model):
    receiver = models.CharField("收货人", max_length=20)
    receiver_mobile = models.CharField("收货手机号", max_length=11)
    receiver_addr = models.CharField("收货地址", max_length=100)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="用户")

    def __str__(self):
        return "收件人：%s"%self.receiver

    class Meta:
        db_table = "tb_addr"
        verbose_name_plural = "收货地址"
